package com.smcdd.cdb.dbo.cdb_dbo.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.MainTenance;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.TainDialogAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;

import java.util.List;

/**
 * 作者：Administrator on 2017/6/27 21:42
 * 邮箱：zhanghuaiha@gmail.com
 */

public class TainDialog extends Dialog {

    private String titleStr;//从外界设置的title文本
    private RecyclerView mRecycler;
    private TainDialogAdapter wifiDialogAdapter;
    private Context mContext;
    private TextView dialog_date,dialog_title,dialog_type;
    List<MainTenance> list;
    private String type;


    public TainDialog(Context context, List<MainTenance> list,String type) {
        super(context, R.style.progress_dialog);
        mContext = context;
        this.list = list;
        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.dialog_list);
//        //按空白处不可以取消动画
//        setCanceledOnTouchOutside(false);
//        wifiDialogAdapter = new WifiDialogAdapter();
//        //初始化界面控件
//        initView();
//        //初始化界面数据
//        initData();
    }

    /**
     * 初始化界面控件的显示数据
     */
    int time = 0;
    int num = 0;
    public void initData() {
        if (list != null) {
            wifiDialogAdapter.addData(list);
            if (list.size() > 0) {

                dialog_date.setText(mContext.getString(R.string.tjrq)+ list.get(0).getDate());
                //在线时长超过两小时
                //设备使用率
                if (type.equals("4")) {
                    for (MainTenance mainTenance : list) {
                        if (Integer.parseInt(mainTenance.getUse_num()) > 0) {
                            num++;
                        }
                    }
                    double c = num * 100.0 / list.size() ;
                    dialog_type.setText("设备使用率："+c+"%");
                }else {
                    for (MainTenance mainTenance : list) {
                        if (mainTenance.getOnline_time() != null) {
                            String online_time = mainTenance.getOnline_time();
                            int i = Integer.parseInt(online_time);
                            if ( i > 120) {
                                time++;
                            }
                        }

                    }
                    dialog_type.setText("在线时长超过两小时："+time);
                }
            }
        }
        String message = "说明语句： \n" +
                "纳入第二日待维护的店铺是由第一日晚10点前设备在线时长决定\r\n所以设备统计时长日期显示第一日";
        dialog_title.setOnClickListener(v -> {
            DialogUtils.TSLeftDialog(mContext,message,true);
        });
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mRecycler = (RecyclerView) findViewById(R.id.dialog_recycler);
        dialog_date = (TextView) findViewById(R.id.dialog_date);
        dialog_title = (TextView) findViewById(R.id.dialog_title);
        dialog_type = (TextView) findViewById(R.id.dialog_type);
        mRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycler.setAdapter(wifiDialogAdapter);
    }

    /**
     * 从外界Activity为Dialog设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleStr = title;
    }

    /**
     * 从外界Activity为Dialog设置dialog的message
     *
     * @param
     */
    public void setDate(List<MainTenance> list) {
        this.list = list;
        wifiDialogAdapter.addData(list);
    }

    @Override
    public void show() {
        super.show();
        setContentView(R.layout.dialog_recycler);
        //按空白处不可以取消动画
        setCanceledOnTouchOutside(true);
        wifiDialogAdapter = new TainDialogAdapter(type);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
    }
}
